/*
 *  NFCSigning - Open source library for signing/validation of NDEF messages
 *  Copyright (C) 2009-2010 The NFCSigning Team
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 */
package sampleappa;

import com.ericsson.nfc.signing.CertificateFetcher;
import com.ericsson.nfc.signing.certificate.CertificateChain;
import com.ericsson.nfc.signing.certificate.X509Certificate;
import com.ericsson.nfc.signing.certificate.X509CertificateChain;
import com.ericsson.nfc.signing.record.URIRecord;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Vector;
import javax.microedition.io.Connector;
import org.bouncycastle.util.encoders.Base64;

/**
 *
 * @author emarkki
 */
class MyCertificateFetcher implements CertificateFetcher {

    private Cert[] certStore;
    
    public MyCertificateFetcher(Cert[] certStore) {
        this.certStore = certStore;
    }

    public CertificateChain fetchChain(URIRecord chainURI) {
        System.out.println("Fetching for URI " + chainURI.getURI());

        CertificateChain chain = findInStore(chainURI);

        if(chain != null) {
            return chain;
        }

        // TODO: Security: limit what types of URI:s that are allowed?

        Vector certsFromUri = new Vector();

        InputStream in = null;
        try {
            in = Connector.openInputStream(chainURI.getURI());

             int ch = 0;
             while(ch != -1) {
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                while ((ch = in.read()) != -1 && ch != '\r' && ch != '\n') {
                    out.write(ch);
                }
                byte[] line = out.toByteArray();
                certsFromUri.addElement(new X509Certificate(Base64.decode(line)));

                if(ch == '\r') {
                    in.skip(1);
                }
             }
        } catch (Throwable ex) {
            ex.printStackTrace();
        } finally {
            try {
                in.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        X509Certificate[] certs = new X509Certificate[certsFromUri.size()];
        for(int i = 0; i < certs.length; i++) {
            certs[i] = (X509Certificate) certsFromUri.elementAt(i);
        }
        
        return new X509CertificateChain(certs);
    }

    private CertificateChain findInStore(URIRecord chainURI) {
        for(int i = 0; i < certStore.length; i++) {
            if(certStore[i].getBuiltInUri().equals(chainURI)) {
                return new X509CertificateChain(certStore[i].getChain());
            }
        }
        return null;
    }
    
}
